home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP9_91.ARJ / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1991-09-12  |  6KB  |  148 lines

  1. /*
  2. **  PushDir() and PopDir()
  3. */
  4.  
  5. #include <dos.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #define DIR_STACK_SIZE  8
  10. #define MAX_FLEN        67
  11.  
  12. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  13.  
  14. int chdrv(int);
  15.  
  16. static int  PushDir_stack_ptr;
  17. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  18.  
  19. /************************************************************************/
  20. /*                                                                      */
  21. /*  PushDir()                                                           */
  22. /*                                                                      */
  23. /*  Like chdir(), except a drive may be specified and the old directory */
  24. /*  is saved.                                                           */
  25. /*                                                                      */
  26. /*  Arguments: 1 - newdir, the buffer containing the new directory name */
  27. /*                                                                      */
  28. /*  Returns:  -1 - stack overflow                                       */
  29. /*             0 - error                                                */
  30. /*             1 - success, still on same drive                         */
  31. /*             2 - success, changed drive                               */
  32. /*                                                                      */
  33. /*  Side effects: Converts name in newdir to upper case and prepends    */
  34. /*                a drive letter.                                       */
  35. /*                                                                      */
  36. /*  CAUTION: Since a drive will be prepended to newdir, it's buffer     */
  37. /*           should be at at least MAX_FLEN long.                       */
  38. /*                                                                      */
  39. /************************************************************************/
  40.  
  41. int PushDir(char *newdir)
  42. {
  43.       char pname[MAX_FLEN];
  44.       char drive[3];
  45.       char *target = &pname[2];
  46.       int i, new_drv = 0, ercode = 0;
  47.       static int init = 0;
  48.  
  49.       if (!init)
  50.             PushDir_stack_ptr = init = -1;
  51.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  52.       {
  53.             ercode = -1;
  54.             goto ErrEx;
  55.       }
  56.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  57.       strupr(PushDir_stack[PushDir_stack_ptr]);
  58.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  59.       drive[2] = '\0';
  60.       if (':' == newdir[1])
  61.       {     /* If a drive is specified                                  */
  62.             strupr(newdir);
  63.             strcpy(pname, newdir);
  64.             if (strchr(target, ':'))      /* if filename is illegal     */
  65.                   goto ErrEx;
  66.             if (*drive != *newdir)
  67.             {
  68.                   if (ERROR == chdrv(newdir[0]))
  69.                   {     /* If the drive is invalid                      */
  70.                         goto ErrEx;
  71.                   }
  72.                   else  new_drv = 1;
  73.             }
  74.       }
  75.       else
  76.       {     /* If a drive isn't specified                               */
  77.             if (!strchr(strupr(newdir), ':'))
  78.             {     /* If legal filename                                  */
  79.                   strcpy(pname, drive);
  80.                   strcat(pname, newdir);
  81.                   strcpy(newdir, pname);
  82.             }
  83.             else
  84.             {     /* If filename is illegal                             */
  85.                   goto ErrEx;
  86.             }
  87.       }
  88.  
  89.       if (*target)
  90.       {
  91.             if (chdir(target))
  92.             {
  93.                   if (1 == new_drv) /* We already changed drives        */
  94.                         chdrv(*drive);    /* Go home before exit        */
  95.                   goto ErrEx;
  96.             }
  97.       }
  98.       return (new_drv + 1);
  99. ErrEx:
  100.       --PushDir_stack_ptr;
  101.       return (ercode);
  102. }
  103.  
  104. /************************************************************************/
  105. /*                                                                      */
  106. /*  PopDir()                                                            */
  107. /*                                                                      */
  108. /*  Like chdir(), except goes to the drive/directory specified on the   */
  109. /*  top of the PushDir stack.                                           */
  110. /*                                                                      */
  111. /*  Arguments: none                                                     */
  112. /*                                                                      */
  113. /*  Returns:  -1 - stack empty                                          */
  114. /*             0 - error - stack pointer unchanged                      */
  115. /*             1 - success, still on same drive                         */
  116. /*             2 - success, changed drive                               */
  117. /*                                                                      */
  118. /*  Side effects: none                                                  */
  119. /*                                                                      */
  120. /*  CAUTION: chdir() or chdrv() should not be called between PushDir-   */
  121. /*           PopDir calls.                                              */
  122. /*                                                                      */
  123. /************************************************************************/
  124.  
  125. int PopDir(void)
  126. {
  127.       char I_am_here[MAX_FLEN], target_drv, *target;
  128.       int new_drv = 0;
  129.  
  130.       if (0 > PushDir_stack_ptr)
  131.             return -1;
  132.       getcwd(I_am_here, MAX_FLEN);
  133.       target = &PushDir_stack[PushDir_stack_ptr][2];
  134.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  135.       if (I_am_here[0] != target_drv)
  136.       {
  137.             if (ERROR == chdrv(target_drv))
  138.                   return 0;
  139.             new_drv = 1;
  140.       }
  141.       if (!chdir(target))
  142.       {
  143.             --PushDir_stack_ptr;
  144.             return (1 + new_drv);
  145.       }
  146.       else  return 0;
  147. }
  148.